package com.zhj.test.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zhj
 */
public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {
        File file1 = new File("E:\\data_file\\log1.txt");
        File file2 = new File("E:\\data_file\\log2.txt");
        // 创建一个输出流
        FileInputStream fileInputStream = new FileInputStream(file1);
        FileChannel fileChannel01 = fileInputStream.getChannel();
        // 创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file2);
        FileChannel fileChannel02 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (true) { // 循环读取
            byteBuffer.clear();
            int read = fileChannel01.read(byteBuffer);
            System.out.println(new String(byteBuffer.array()));
            if (read == -1) {
                break;
            }
            // 将buffer 写入到 fileChannel02
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
